package com.kifas.service.dataaccess;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kifas.entity.KontrakEntity;
import com.kifas.entity.KontrakPembelianEntity;
import com.kifas.entity.SupplierEntity;

@Repository
public class KontrakDao {
	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(KontrakDao.class);

	private static String ALL_KONTRAK = "from KontrakEntity k ";
	private static String ALL_KONTRAK_PEMBELIAN = "from KontrakPembelianEntity kp ";
	private static String KONTRAK_BY_ID = ALL_KONTRAK
			+ "where k.idKontrak = :idKontrak ";
	private static String KONTRAK_BY_SUPPLIER = ALL_KONTRAK
			+ "where k.supplier = :supplier ";
	private static String KONTRAK_AND_DETAIL = ALL_KONTRAK
			+ "left join fetch KontrakPembelianEntity kp ";
	private static String KONTRAK_OPEN_STATUS = KONTRAK_AND_DETAIL
			+ "where kp.closed = false ";
	private static String KONTRAK_CLOSED_STATUS = KONTRAK_AND_DETAIL
			+ "where kp.closed = true ";
	private static String KONTRAK_PEMBELIAN_BY_KONTRAK = ALL_KONTRAK_PEMBELIAN
			+ "where kp.kontrak = :kontrak ";

	@Autowired
	private SessionFactory sessionFactory;

	public void insert(KontrakEntity kontrak) {
		sessionFactory.getCurrentSession().save(kontrak);
		for (KontrakPembelianEntity kontrakPembelian : kontrak
				.getKontrakPembelians()) {
			sessionFactory.getCurrentSession().save(kontrakPembelian);
		}
	}

	@SuppressWarnings("unchecked")
	public List<KontrakEntity> getKontraks() {
		return sessionFactory.getCurrentSession().createQuery(ALL_KONTRAK)
				.list();
	}

	public KontrakEntity getKontrak(Long idKontrak) {
		return (KontrakEntity) sessionFactory.getCurrentSession()
				.createQuery(KONTRAK_BY_ID).setLong("idKontrak", idKontrak)
				.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<KontrakEntity> getKontraksBySupplier(SupplierEntity supplier) {
		return sessionFactory.getCurrentSession()
				.createQuery(KONTRAK_BY_SUPPLIER)
				.setEntity("supplier", supplier).list();
	}

	@SuppressWarnings("unchecked")
	public List<KontrakEntity> getOpenKontraks() {
		return sessionFactory.getCurrentSession()
				.createQuery(KONTRAK_OPEN_STATUS).list();
	}

	@SuppressWarnings("unchecked")
	public List<KontrakEntity> getClosedKontraks() {
		return sessionFactory.getCurrentSession()
				.createQuery(KONTRAK_CLOSED_STATUS).list();
	}

	@SuppressWarnings("unchecked")
	public List<KontrakPembelianEntity> getKontrakPembelian(
			KontrakEntity kontrak) {
		return sessionFactory.getCurrentSession()
				.createQuery(KONTRAK_PEMBELIAN_BY_KONTRAK)
				.setEntity("kontrak", kontrak).list();
	}

	public void updateKontrakToClosed(Long idKontrakPembelian) throws Exception {
		KontrakPembelianEntity kontrakPembelian = (KontrakPembelianEntity) sessionFactory
				.getCurrentSession().get(KontrakPembelianEntity.class,
						idKontrakPembelian);
		kontrakPembelian.setClosed(true);
		sessionFactory.getCurrentSession().update(kontrakPembelian);
	}
}
